﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using InternetTime;
using Microsoft.Xna.Framework;

namespace TestXNA
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            DataContext = App.ViewModel;

            App.ViewModel.Posicion = 1;
            if (App.ViewModel.sync == null)
            {
                App.ViewModel.sync = new Sync();
                App.ViewModel.sync.CrearListener();
            }
        }

        // Simple button Click event handler to take us to the second page
        private void Button_Click(object sender, RoutedEventArgs e)
        {
//            SNTPClient cliente = new SNTPClient("ntp.shoa.cl"); // "cl.pool.ntp.org");
////            double prec = 0;
//            int prec = 0;
//            int offset = 0;
//            int num = 5;

//            for (int i = 0; i < num; i++)
//            {
//                try
//                {
//                    cliente.Connect(false);
//                }
//                catch (Exception ex)
//                {
//                    // obtengamos las X lecturas.
//                    i--;
//                    continue;
//                }
////                                offset += cliente.LocalClockOffset;
//                if (prec == 0 || prec < cliente.RoundTripDelay)
//                {
//                    offset = cliente.LocalClockOffset;
//                    prec = cliente.RoundTripDelay;
//                }
//                // calculemos la diferencia entre la hora del sistema y la hora actual
//            }
////            offset /= num;
//            App.ViewModel.ClockOffset = offset;

            //App.ViewModel.Posicion = 1;
            //if (App.ViewModel.sync == null)
            //{
            //    App.ViewModel.sync = new Sync();
            //    App.ViewModel.sync.CrearListener();
            //}
            //App.ViewModel.sync.RequestTime();
            // s.SendTime();

            if (App.ViewModel.Posicion > 1)
            {
                App.ViewModel.cameraPosition = new Vector3(2400.0f * (App.ViewModel.Posicion - 1), 50.0f, 5000.0f);
                App.ViewModel.cameraView = new Vector3(2400.0f * (App.ViewModel.Posicion - 1), 0, 0);
            }
            else
            {
                App.ViewModel.cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);
                App.ViewModel.cameraView = Vector3.Zero;
            }

            App.ViewModel.sync.MejorLatencia = 0.0;

            if (App.ViewModel.Posicion > 1)
                App.ViewModel.sync.RequestTime();

            NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        }
    }
}